package com.yanqu.road.server.cmd.activity.feast.pvp;

import com.yanqu.road.entity.activity.feast.config.ShanHeFeastConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ShanHeFeastProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.feast.CrossFeastMgr;
import com.yanqu.road.server.manager.activity.feast.activity.CrossFeastActivity;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C_SHAN_HE_FEAST_LOCK_TARGET, desc = "锁定玩家")
public class CrossFeastLockTargetCmd extends CrossCommand {

    long playerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ShanHeFeastProto.CrossFeastLockTargetReqMsg reqMsg = ShanHeFeastProto.CrossFeastLockTargetReqMsg.parseFrom(packet.getBytes());
        CrossFeastActivity activity = CrossFeastMgr.getActivity(reqMsg.getActivityId());
        if (activity == null) {
            ret = GameErrorCode.E_SHAN_HE_FEAST_ACTIVITY_NOT_FOUND;
            return;
        }
        ShanHeFeastConfig config = activity.getConfig();
        if (config == null) {
            ret = GameErrorCode.E_SHAN_HE_FEAST_CONFIG_NOT_FOUND;
            return;
        }
        ret = activity.lockUserByCmd(serverId, playerId, reqMsg.getUserId());
    }

    @Override
    public void sendData() {
        if (ret != 0) {
            ShanHeFeastProto.FeastLockTargetRespMsg.Builder respMsg = ShanHeFeastProto.FeastLockTargetRespMsg.newBuilder();
            respMsg.setRet(ret);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_SHAN_HE_FEAST_LOCK_TARGET, respMsg));
        }
    }

}
